using System;
using System.Globalization;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using mojoPortal.Business.WebHelpers;
using eStore.Business;

namespace eStore.UI
{

    public partial class eStoreCartModule : SiteModuleControl
    {
        protected ModuleTitleControlCustom TitleControl;
        protected Store store;
        protected CultureInfo currencyCulture = CultureInfo.CurrentCulture;
        protected eStoreConfiguration config = new eStoreConfiguration();
        private int loadedStoreModuleId = -1;
        private int loadedStorePageId = -1;

        #region OnInit

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Site != null && Site.DesignMode) { return; }

            Load += Page_Load;
            LoadSettings();
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Site != null && Site.DesignMode) { return; }

            SetupCss();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateLabels()
        {
            lnkCart.ModuleID = loadedStoreModuleId;
            lnkCart.PageID = loadedStorePageId;
        }

        private void PopulateControls()
        {

        }

        private void LoadSettings()
        {
            config = new eStoreConfiguration(Settings);
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            string loadedCategory = config.EStoreLoaderSelectorSetting;
            if (!loadedCategory.Equals(string.Empty))
            {
                int.TryParse(loadedCategory.Remove(loadedCategory.IndexOf("-")), out loadedStoreModuleId);
                int.TryParse(loadedCategory.Remove(0, loadedCategory.IndexOf("-") + 1), out loadedStorePageId);
            }
            store = new Store(siteSettings.SiteGuid, loadedStoreModuleId);
            
            pnlWrapper.CssClass = config.CustomCssClassSetting;
            currencyCulture = ResourceHelper.GetCurrencyCulture(siteSettings.GetCurrency().Code);

        }

        protected virtual void SetupCss()
        {
            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("styleestore") != null) return;
            Literal cssLink = new Literal
            {
                ID = "styleestore",
                Text = @"<link href='"
                       + SiteUtils.GetSkinBaseUrl()
                       +
                       @"styleestore.css' type='text/css' rel='stylesheet' media='screen' />"
            };

            stylesheet.Controls.Add(cssLink);
        }

    }
}